#! /usr/bin/perl -w
use strict;

# =====================================================================================
#
#       Filename:  FindMenu
#
#    Description:  Find Entrance Program and Menu for each progress source code.
#           
#Dependent files:  1.program.lst:
#                   A list of progress source file names used to find menu.
#                  2.menu.lst:
#                   Run generate_menu_list.p to create a menu.lst for your own environment.
#
#        Version:  1.0
#        Created:  2011/12/27 09:45:26
#       Revision:  1
#
#         Author:  Wade Xu (jxx), jxx@qad.com
#                   Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA. 
#
# =====================================================================================


# Global Variable Definitions
our @programList;
our %hashMenu;

# Main Routine
    #Open Program List
    my $programListFile = "program.lst";
    open (SOURCEFILE, $programListFile) || die "Cannot open file $programListFile.";
    @programList = <SOURCEFILE>;

    print "\nBegin $programListFile......\n";
    
    my $program;
    foreach $program(@programList)
    {
        chomp($program);
        
        print "Processing $program ......\n";
        my @resultLines={};
        my $line="";
        @resultLines = `whereused erp-trunk $program`;
        
        foreach $line(@resultLines)
        {
            chomp($line);
            $line =~ s/\. //g;
            $line =~ s/  \*.* \*//g;

            if ($line =~ /MENU (.*)  \(mnd_select=(.*)\)/)
            {
                my $menu_nbr = $1;
                my $menu_select = $2;
                $line =~ s/MENU //g;
                $line =~ s/  \(mnd_select=/\./g;
                $line =~ s/\)//g;
                print "$line\n";
                if ( not exists($hashMenu{$line}) )
                {
                    #generate menu details
                    my @menuList;
                    my $menuLine;
                    my $menuListFile = "menu.lst";
                    open (MENUFILE, $menuListFile) || die "Cannot open file $menuListFile.\nUse generate_menu_list.p to create list.\n";
                    @menuList = <MENUFILE>;
                    foreach $menuLine(@menuList)
                    {
                        chomp($menuLine);
                        if ( $menuLine =~ /$menu_nbr\.$menu_select +(.*) +\"(.*)\"/ && $menuLine !~ /.$menu_nbr\.$menu_select/ )
                        {
                            $hashMenu{$line} .= "$2\t$1\t,";
                        }
                    }
                }
                $hashMenu{$line} .= ",$program";
            }
        }
    }

    open (OUTFILE, ">usedResult");
    print OUTFILE ("\t\tMENU ID\t\t EXEC PROGRAM\t\tMENU LABEL\t\t\t\t\t\t\t\tAFFECT PROGRAM\n");
    print OUTFILE ("-------------------- -------------  ------------------------------------------  -------------\n");
    while (my ($k, $v) = each %hashMenu) 
    {
        printf OUTFILE ("%20s\t%s\n",$k,$v);
    }

    close OUTFILE;
    close SOURCEFILE;
    print "End $programListFile......\n\n";



